import time

from selenium.webdriver.common.by import By

from base.base_page import BasePage
from driver_utils import DriverUtils


class AddVIPPage(BasePage):

    def login_page(self):
        self.get_url("http://hmshop-test.itheima.net/index.php/Admin/Admin/login")
        self.element_send_keys(By.CLASS_NAME, "input-text", "admin")
        self.element_send_keys(By.NAME, "password", "123456")
        self.element_send_keys(By.ID, "vertify", "8888")
        self.element_click(By.NAME, "submit")

    def add_vip(self, nickname, password, mobile):
        self.element_click(By.LINK_TEXT, "会员")
        self.element_click(By.LINK_TEXT, "会员列表")
        self.element_iframe(By.ID, "workspace")
        self.element_click(By.CSS_SELECTOR, "div[title='添加会员']")
        self.element_send_keys(By.ID, "nickname", nickname)
        self.element_send_keys(By.ID, "password", password)
        self.element_send_keys(By.ID, "mobile", mobile)
        self.element_click(By.ID, "submitBtn")

    def add_y_or_n(self, mobile):
        self.element_down_index(By.ID, "search_type", 1)
        self.element_send_keys(By.ID, "search_key", mobile)
        self.element_click(By.CSS_SELECTOR, "input[type='button'][value='搜索']")
        return self.element_text(By.ID, "flexigrid")

    def close_login(self):
        self.element_default()
        self.element_move_to(By.CLASS_NAME, "bgdopa-t")
        self.element_click(By.CLASS_NAME, "login-out")

    def add_vip_page(self, nickname, password, mobile):
        self.login_page()
        self.add_vip(nickname, password, mobile)
        text = self.add_y_or_n(mobile)
        self.close_login()
        return text


if __name__ == '__main__':
    AddVIPPage().add_vip_page("13214785223", "123456", "13214785223")
    DriverUtils.quit_driver()